package com.TRoom.controller;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.IOException;
import java.io.Reader;
import java.util.*;


@Controller
public class TableController {

    @RequestMapping("hello")
    @ResponseBody
    public String Hello(){
        return "Hello";
    }

    @RequestMapping("proportion")
    public String proportion(Model model) throws Exception{
        SqlSession sqlSession = null;
        sqlSession = getSession();
        int androidCount = sqlSession.selectOne("countAndroidAndIphone","android");
        int iphoneCount = sqlSession.selectOne("countAndroidAndIphone","iPhone");
        int count = sqlSession.selectOne("getCount");

        model.addAttribute("androidCount",androidCount);
        model.addAttribute("iphoneCount",iphoneCount);
        model.addAttribute("count",count-iphoneCount-androidCount);
        return "phoneproportion";
    }

    @RequestMapping("Top10Url")
    public String Top10Url(Model model) throws Exception{
        SqlSession session = getSession();
        List<String> urls = session.selectList("urlTop10");
        Map<String,Integer> ts = new TreeMap<String, Integer>();
        for (String url:urls){
            if(url.split(" ")[1] != null){
                String temp = url.split(" ")[1];
                if(ts.containsKey(temp)){
                    ts.put(temp,ts.get(temp)+1);
                }else{
                    ts.put(temp,1);
                }
            }
        }
        //这里将map.entrySet()转换成list
        List<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(ts.entrySet());
        //然后通过比较器来实现排序
        Collections.sort(list,new Comparator<Map.Entry<String,Integer>>() {
            //升序排序
            public int compare(Map.Entry<String, Integer> o1,
                               Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        int i = 0;
        List<Map.Entry<String,Integer>> res = new ArrayList<Map.Entry<String, Integer>>();
        List<String> us = new ArrayList<String>();
        List<Integer> vs = new ArrayList<Integer>();
        for(Map.Entry<String,Integer> mapping:list){
            if(i++ < 10) {
                us.add(mapping.getKey());
                vs.add(mapping.getValue());
                res.add(mapping);
                System.out.println(i+":"+mapping.getKey()+":"+mapping.getValue());
            }else {
                break;
            }
        }
        model.addAttribute("res",res);
        model.addAttribute("us",us);
        model.addAttribute("vs",vs);
        return "top10url";
    }

    @RequestMapping("Top10ip")
    public String Top10ip(Model model){
        SqlSession sqlSession = getSession();
        List<TreeMap<String,Long>> ips = sqlSession.selectList("ipSortTop10");
        List<Row> rows = new ArrayList<Row>();
        Row row;

        int i = 1;
        Map<Long, Long> ts = new TreeMap<Long, Long>();
        for (TreeMap<String,Long> ip:ips){

            //ts.put(ip.get("userIP"),ip.get("count(userIP)"));
            System.out.println(String.valueOf(ip.get("count(userIP)")));
            row = new Row(i++,String.valueOf(ip.get("userIP")),String.valueOf(ip.get("count(userIP)")));
            rows.add(row);
        }
//        List<Map.Entry<Long,Long>> list = new ArrayList<Map.Entry<Long,Long>>(ts.entrySet());
//        //然后通过比较器来实现排序
//        Collections.sort(list,new Comparator<Map.Entry<Long,Long>>() {
//            //升序排序
//            public int compare(Map.Entry<Long, Long> o1,
//                               Map.Entry<Long, Long> o2) {
//                return o2.getValue().compareTo(o1.getValue());
//            }
//
//        });
//        int i = 0;
//        List<Map.Entry<Long,Long>> res = new ArrayList<Map.Entry<Long, Long>>();
//        for(Map.Entry<Long,Long> mapping:list){
//            if(i++ < 10) {
//                res.add(mapping);
//                System.out.println(i+":"+mapping.getKey()+":"+mapping.getValue());
//            }else {
//                break;
//            }
//        }
//        model.addAttribute("res",res);
        model.addAttribute("rows",rows);
        return "top10ip";
    }


    private SqlSession getSession(){
        SqlSession sqlSession = null;
        String resources = "mybatis-config.xml";
        Reader reader = null;
        try{
            reader = Resources.getResourceAsReader(resources);
        }catch (IOException e){
            e.printStackTrace();
        }

        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);

        sqlSession = sqlMapper.openSession();
        return sqlSession;
    }
}
